package com.waner.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.waner.reggie.common.BaseContext;
import com.waner.reggie.common.R;
import com.waner.reggie.dto.OrdersDto;
import com.waner.reggie.entity.*;
import com.waner.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;


    @GetMapping("/page")
    public R<Page<OrdersDto>> page(int page, int pageSize, String number, LocalDateTime beginTime, LocalDateTime endTime){
        // 进行条件分页查询（name+time）
        Page<Orders> pageOrders = ordersService.pageWithNumberTime(page,pageSize,number,beginTime,endTime);

        return R.success(null);
    }

    /**
     * 新增订单 + 订单详情（订单内容）+ 删除购物车信息
     * @param order
     * @return
     */
    @PostMapping("/submit")
    public R<String> save(@RequestBody Orders order){

        ordersService.saveWithDetailAndSCart(order);

        return R.success("下单成功！");
    }

    @GetMapping("/userPage")
    public R<Page<OrdersDto>> page(int page,int pageSize){
        log.info("用户订单的分页查询...");

        Page<OrdersDto> pageInfo = ordersService.pageWithDetais(page,pageSize);

        return R.success(pageInfo);
    }

}
